//
//  RBVideoFastForwardView.m
//  RBVideoPlayer
//
//  Created by Lan on 2019/2/26.
//  Copyright © 2019 SummerTea. All rights reserved.
//

#import "RBVideoFastForwardView.h"

@interface RBVideoFastForwardView()

@property(nonatomic, strong) UIImageView *imgView;
@property(nonatomic, strong) UILabel *textL;

@property(nonatomic, copy) void(^endBlock)(NSInteger time);
@property(nonatomic, assign) NSInteger time;

@end

@implementation RBVideoFastForwardView

#pragma mark - System

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.frame = CGRectMake(0, 0, 120, 70);
        self.backgroundColor = [UIColor colorWithRed:0/256.0f green:0/256.0f blue:0/256.0f alpha:0.8];
        self.layer.cornerRadius  = 4;
        self.layer.masksToBounds = YES;
        self.imgView.hidden = NO;
        self.textL.hidden = NO;
        self.hidden = YES;
    }
    return self;
}

#pragma mark - Public

- (void)showWithBeginTime: (NSInteger)beginTime totalTime: (NSInteger)totalTime progress: (CGFloat)progress endBlock: (void(^)(NSInteger time))endBlock
{
    self.hidden = NO;
    CGRect bounds = self.superview.bounds;
    CGFloat width = bounds.size.width;
    CGFloat height = bounds.size.height;
    if (!CGAffineTransformEqualToTransform(self.superview.transform, CGAffineTransformIdentity))
    {//旋转屏幕了
        self.center = CGPointMake(MAX(width, height)/2, MIN(width, height)/2);
    }
    else
    {
        self.center = CGPointMake(width/2, height/2);
    }
    [self.superview bringSubviewToFront:self];
    
    self.endBlock = endBlock;
    NSInteger time = 0;
    if (totalTime < 60*10)
    {
        time = beginTime + progress*totalTime;
    }
    else if (totalTime > 60*60)
    {
        time = beginTime + progress*(totalTime/2);
    }
    else
    {
        time = beginTime + progress*60*30;
    }
    time = MAX(time, 0);
    time = MIN(time, totalTime);
    self.time = time;
    
    if (progress > 0)
    {
        self.imgView.image = [UIImage imageNamed:@"RBMediaPlayer.bundle/progressRight"];
    }
    else
    {
        self.imgView.image = [UIImage imageNamed:@"RBMediaPlayer.bundle/progressLeft"];
    }
    self.textL.text = [NSString stringWithFormat:@"%02d:%02d/%02d:%02d", time/60, time%60, totalTime/60, totalTime%60];
}

- (void)hide
{
    self.hidden = YES;
    if (self.endBlock)
    {
        self.endBlock(self.time);
    }
    self.endBlock = nil;
}

#pragma mark - Getter

- (UIImageView *)imgView
{
    if (!_imgView)
    {
        self.imgView = ({
            UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, self.frame.size.width, 40)];
            view.contentMode = UIViewContentModeScaleAspectFit;
            [self addSubview:view];
            view;
        });
    }
    return _imgView;
}

- (UILabel *)textL
{
    if (!_textL)
    {
        self.textL = ({
            UILabel *view = [[UILabel alloc] initWithFrame:CGRectMake(0, 45, self.frame.size.width, 16)];
            view.textColor = [UIColor whiteColor];
            view.font = [UIFont systemFontOfSize:14];
            view.numberOfLines = 1;
            view.textAlignment = NSTextAlignmentCenter;
            [self addSubview:view];
            view;
        });
    }
    return _textL;
}

@end
